Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices



Public Class FrmSearchPat

    Public Class ColoredTextBoxColumn
        ' ColoredTextBoxColumn is derived from the DataGridTextBoxColumn class.
        Inherits DataGridTextBoxColumn
        Private mMyDataGrid As DataGrid
        Public Property OwnerDG() As DataGrid
            Get
                Return mMyDataGrid
            End Get
            Set(ByVal Value As DataGrid)
                mMyDataGrid = Value
            End Set
        End Property
        Protected Overloads Overrides Sub Paint(ByVal graph As Graphics, _
            ByVal rectbounds As Rectangle, ByVal curmngrSrc As _
            CurrencyManager, ByVal RowNumber As Integer, ByVal _
            ForeColorBrush As Brush, ByVal BackColorBrush As Brush, _
            ByVal AlignmentRight As Boolean)

            Dim ObjVal As Object
            ObjVal = Me.GetColumnValueAtRow(curmngrSrc, RowNumber)

            If Not (IsNothing(ObjVal) Or IsDBNull(ObjVal)) Then
                If Me.OwnerDG.CurrentRowIndex = RowNumber Then
                    BackColorBrush = Brushes.Yellow
                    ForeColorBrush = Brushes.Red
                Else
                    BackColorBrush = Brushes.White
                    ForeColorBrush = Brushes.Black
                End If
            End If

            MyBase.Paint(graph, rectbounds, curmngrSrc, RowNumber, _
                BackColorBrush, ForeColorBrush, AlignmentRight)
        End Sub

        'Protected Overloads Overrides Sub Edit(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
        '    MyBase.Edit(source, rowNum, bounds, False, instantText, cellIsVisible)
        'End Sub

    End Class
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub
    Private Sub LoadDataGrid(ByVal vSort)

        Dim cnSQL As New SqlConnection(GV_CONNETION)
        cnSQL.Open()

        Dim sqlCmd = New SqlCommand("spSearchPatient", cnSQL)
        sqlCmd.CommandType = CommandType.StoredProcedure
        sqlCmd.Parameters.Add("@SORT", SqlDbType.VarChar)
        sqlCmd.Parameters("@SORT").Value = vSort
        Dim sqlAdp = New SqlDataAdapter(sqlCmd)
        Dim ApptList_Data = New DataSet()
        sqlAdp.Fill(ApptList_Data, "patients")


        '----------------------------------------------
        Dim ts As New DataGridTableStyle()
        ts.MappingName = ApptList_Data.Tables(0).TableName
        With ts
            .MappingName = "PATIENTS"
            .BackColor = System.Drawing.Color.White
            .ForeColor = System.Drawing.Color.DarkSlateBlue
            .GridLineColor = System.Drawing.Color.MediumSlateBlue
            .HeaderBackColor = System.Drawing.Color.Lavender
            .HeaderForeColor = System.Drawing.Color.SlateBlue
            .RowHeadersVisible = False
            .PreferredRowHeight = 15
        End With

        '...highlight row 
        Dim numCols As Integer
        numCols = ApptList_Data.Tables(0).Columns.Count
        Dim aColumnTextColumn As ColoredTextBoxColumn 'DataGridTextBoxColumn

        Dim i As Integer
        i = 0

        Do While (i < numCols)
            aColumnTextColumn = New ColoredTextBoxColumn()
            aColumnTextColumn.OwnerDG = Me.DataGrid1
            ' aColumnTextColumn.ConditionalText = Me.tbFind
            Select Case i
                Case 0

                    aColumnTextColumn.HeaderText = "PATIENT_ID"
                    aColumnTextColumn.MappingName = "Pt_id1"
                    aColumnTextColumn.Width = 0
                Case 1
                    aColumnTextColumn.HeaderText = "NAME"
                    aColumnTextColumn.MappingName = "PtName"
                    aColumnTextColumn.Width = 300
                Case 2
                    aColumnTextColumn.HeaderText = "NRIC"
                    aColumnTextColumn.MappingName = "ptNRIC"
                    aColumnTextColumn.Width = 100
                Case 3
                    aColumnTextColumn.HeaderText = "MR ID"
                    aColumnTextColumn.MappingName = "ptMRID"
                    aColumnTextColumn.Width = 100
                Case 4
                    aColumnTextColumn.HeaderText = "CONTACT NO."
                    aColumnTextColumn.MappingName = "Phone"
                    aColumnTextColumn.Width = 120
                    aColumnTextColumn.TextBox.Multiline = True
                Case 5
                    aColumnTextColumn.HeaderText = "DOB"
                    aColumnTextColumn.MappingName = "PtBirthDate"
                    aColumnTextColumn.Format = "dd/MM/yyyy"
                    aColumnTextColumn.Width = 100
                Case 6
                    aColumnTextColumn.HeaderText = "GENDER"
                    aColumnTextColumn.MappingName = "PtGender"
                    aColumnTextColumn.Width = 100
                Case 7
                    aColumnTextColumn.HeaderText = "ADDRESS"
                    aColumnTextColumn.MappingName = "ADDR"
                    aColumnTextColumn.Width = 300
            End Select
            'aColumnTextColumn.HeaderText = ApptList_Data.Tables(0).Columns(i).ColumnName
            'aColumnTextColumn.MappingName = ApptList_Data.Tables(0).Columns(i).ColumnName
            ts.GridColumnStyles.Add(aColumnTextColumn)
            i = (i + 1)
        Loop


        'DataGrid1.DataBindings.Add(ApptList_Data)
        DataGrid1.TableStyles.Clear()     'clear the contents for second try.
        DataGrid1.TableStyles.Add(ts)
        DataGrid1.DataSource = ApptList_Data.Tables("PATIENTS")


    End Sub



    Private Sub ReLoadDataGrid(ByVal vSort)

        Dim cnSQL As New SqlConnection(GV_CONNETION)
        cnSQL.Open()

        Dim sqlCmd = New SqlCommand("spSearchPatient", cnSQL)
        sqlCmd.CommandType = CommandType.StoredProcedure
        sqlCmd.Parameters.Add("@SORT", SqlDbType.VarChar)
        sqlCmd.Parameters("@SORT").Value = vSort
        Dim sqlAdp = New SqlDataAdapter(sqlCmd)
        Dim ApptList_Data = New DataSet()
        sqlAdp.Fill(ApptList_Data, "patients")

        DataGrid1.DataSource = ApptList_Data.Tables("PATIENTS")


    End Sub
    Private Sub FrmSearchPat_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LblSearch.Text = ""
        LoadDataGrid("NAME")
        GV_PATIENT_SORT = "PtName"
    End Sub

    Private Sub rbName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbName.CheckedChanged
        ReLoadDataGrid("NAME")
        GV_PATIENT_SORT = "PtName"
    End Sub

    Private Sub rbNRIC_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbNRIC.CheckedChanged
        ReLoadDataGrid("NRIC")
        GV_PATIENT_SORT = "ptNRIC"
    End Sub

    Private Sub rbMRID_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbMRID.CheckedChanged
        ReLoadDataGrid("MRID")
        GV_PATIENT_SORT = "ptMRID"
    End Sub

    Private Sub rbContactNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbContactNo.CheckedChanged
        ReLoadDataGrid("PHONE")
        GV_PATIENT_SORT = "Phone"
    End Sub

    Private Sub Panel4_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel4.Paint

    End Sub

    Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged

    End Sub

    Private Sub DataGrid1_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.DragLeave

    End Sub

    Private Sub DataGrid1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGrid1.KeyDown
        ' LblSearch.Text = LblSearch.Text + Chr(e.KeyValue)
    End Sub
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _
                           ByVal keyData As Keys) As Boolean
        Const WM_KEYDOWN As Integer = &H100
        Const WM_SYSKEYDOWN As Integer = &H104
        If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
            Select Case (keyData)
                Case Keys.A To Keys.Z
                    LblSearch.Text = LblSearch.Text + Chr(keyData)
                Case Keys.D0 To Keys.D9
                    LblSearch.Text = LblSearch.Text + Chr(keyData)
                Case Keys.NumPad0 To Keys.NumPad9
                    LblSearch.Text = LblSearch.Text + Chr(keyData - 48)
                Case Keys.Decimal
                    LblSearch.Text = LblSearch.Text + "."
                Case Keys.Add Or Keys.Oemplus
                    LblSearch.Text = LblSearch.Text + "+"
                Case Keys.OemMinus
                    LblSearch.Text = LblSearch.Text + "-"
                Case Keys.Multiply
                    LblSearch.Text = LblSearch.Text + "*"
                Case Keys.Divide
                    LblSearch.Text = LblSearch.Text + "/"
                Case Keys.Oemcomma
                    LblSearch.Text = LblSearch.Text + ","

                Case Keys.Space
                    LblSearch.Text = LblSearch.Text + " "
                Case 109
                    LblSearch.Text = LblSearch.Text + "-"
                Case 107
                    LblSearch.Text = LblSearch.Text + "+"
                Case 190
                    LblSearch.Text = LblSearch.Text + "."
                Case 191
                    LblSearch.Text = LblSearch.Text + "/"
                Case Keys.Back
                    If Len(LblSearch.Text) > 0 Then LblSearch.Text = Mid(LblSearch.Text, 1, Len(LblSearch.Text) - 1)

                    'Case Keys.Down
                    '    Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember), CurrencyManager)
                    '    DataGrid1.UnSelect(cm.Position)
                    '    cm.Position = cm.Position + 1
                    '    DataGrid1.Select(cm.Position)

                    'Case Keys.Up
                    '    Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember), CurrencyManager)
                    '    DataGrid1.UnSelect(cm.Position)
                    '    cm.Position = cm.Position - 1
                    '    DataGrid1.Select(cm.Position)
                Case Else
                    'LblSearch.Text = LblSearch.Text + Str(keyData)
                    'MsgBox(Chr(keyData) & "," & keyData)
            End Select
            Lookup_Patient()
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
    Private Sub Lookup_Patient()

        Dim vCode As String
        Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember), CurrencyManager)
        Dim dv As DataView = CType(cm.List, DataView)
        Dim drv() As DataRowView
        Dim vFilter As String

        'Sort the DataView column to search
        vCode = UCase(Trim(LblSearch.Text))
        dv.Sort = GV_PATIENT_SORT.ToString

        'Search the row
        'drv = dv.RowFilter
        vFilter = GV_PATIENT_SORT & " LIKE '" & vCode.ToString & "*'"
        dv.RowFilter = vFilter
        drv = dv.FindRows(vCode.ToString)
        If drv.Length > 0 Then
            cm.Position = cm.List.IndexOf(drv(0))
            DataGrid1.Select(cm.Position)
        Else
            'MsgBox(" not found.")
            'DataGrid1.Select(2)
        End If


    End Sub
    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        GetCellValue()
    End Sub
    Private Sub GetCellValue()
        Try
            Dim myCell As New DataGridCell()
            Dim myCellNRIC As New DataGridCell()
            Dim myCellNAME As New DataGridCell()

            ' Use an arbitrary cell.
            myCell.RowNumber = DataGrid1.CurrentRowIndex
            myCellNRIC.RowNumber = DataGrid1.CurrentRowIndex
            myCellNAME.RowNumber = DataGrid1.CurrentRowIndex


            myCell.ColumnNumber = 0 'patient ID
            myCellNRIC.ColumnNumber = 2    'NRIC
            myCellNAME.ColumnNumber = 1    'NAME


            GV_CUR_PATID = DataGrid1(myCell)
            GV_CUR_PATNRIC = DataGrid1(myCellNRIC)
            GV_CUR_PATNAME = DataGrid1(myCellNAME)
            DataGrid1.Invalidate()
            Close()
            'FrmMain.Button1.Text = GV_CUR_PATNRIC + " " + GV_CUR_PATNAME
            'FrmMain.Button1.Refresh()
        Catch ex As Exception
            Dim vStrErr As String
            vStrErr = ex.Message & " ||GetCellValue||"
            Write_ErrLog(vStrErr)
            Exit Sub
        End Try
    End Sub
    Private Sub Write_ErrLog(ByVal vStrErr)
        'Write error log into errlog.txt
        Dim strAppPath
        strAppPath = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\") - 1)

        Dim myStreamWriter As New System.IO.StreamWriter(strAppPath & "\Errlog.txt", True)
        myStreamWriter.WriteLine(Now & "  " & vStrErr)
        myStreamWriter.Flush()
        myStreamWriter.Close()
    End Sub



    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub
End Class